﻿
using System;
using System.Linq;
using System.Collections.Generic;

using SubSonic.Query;

using Northwind.Data;

namespace SubsonicSqlserverDemo
{
    sealed class DepartmentTester
    {
        // ********************************************************* //
        #region [ helper methods ]

        private static void PrintDeparment(TestDepartment department)
        {
            Console.WriteLine("[{0}]-{1}", department.Id.ToString(), department.Name);
        }

        private static TestDepartment[] CreateDepartments(int number)
        {
            TestDepartment[] departments = new TestDepartment[number];
            for (int index = 0; index < number; ++index)
            {
                departments[index] = new TestDepartment { Name = string.Format("test-{0}", index.ToString()) };
            }
            return departments;
        }

        #endregion

        // ********************************************************* //
        #region [ test ]

        private static void TestSingleInsert()
        {
            TestDepartment department = new TestDepartment
            {
                Name = "Test"
            };
            department.Add();
            PrintDeparment(department);
        }

        private static void TestBatchInsert()
        {
            TestDepartment[] departments = CreateDepartments(3);

            NorthwindDB db = new NorthwindDB();
            BatchQuery batchquery = new BatchQuery(db.Provider, db.QueryProvider);

            /*
            IEnumerable<Insert> inserts = from d in departments
                                          select db.Insert.Into<TestDepartment>(x => x.Name).Values(d.Name);
             * */
            IEnumerable<Insert> inserts = from d in departments
                                          select db.Insert.Into<TestDepartment>(TestDepartmentTable.NameColumn).Values(d.Name);

            foreach (Insert insert in inserts)
                batchquery.Queue(insert);

            batchquery.Execute();

            /*
            foreach (Insert insert in inserts)
                insert.Execute();
             * */

            Console.WriteLine("DONE !!!");
        }

        #endregion

        // ********************************************************* //
        #region [ main entry ]

        public static void TestMain()
        {
            // TestSingleInsert();
            TestBatchInsert();
        }

        #endregion
    }
}